// components/product-list-item/index.js
import {accLess, numberToFixed} from "../../utils/Util";
import {getStorageSync} from "../../utils/Storage"
Component({
    /*
    * 接收外部样式
    * */
    options: {
        addGlobalClass: true,
        multipleSlots: true
    },

    /**
     * 组件的属性列表
     */
    properties: {
        isLink: {
            type: Boolean,
            value: true
        },
        customStyle: { //自定义样式
            type: String,
            value: ""
        },
        customClass: { //自定义class
            type: String,
            value: ""
        },
        summary: { //概要
            type: Object,
            value: {}
        },
        showRightIcon: {
            type: Boolean,
            value: true
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _summary: {},
        _showPoint: false
    },

    attached: function () {
        this.checkUser();
    },

    pageLifetimes: {
        show: function () {
            this.checkUser();
        }
    },

    /*
    * 数据监听
    * */
    observers: {
        "summary": function (summary) {
            if (summary && Object.keys(summary).length > 0) {
                let _summary = summary;
                _summary.n_added_nav = (function () {
                    let val = _summary.n_added_nav;
                    if (val) {
                        let _val = accLess(val, 1, true);
                        return numberToFixed(_val) + "%"
                    } else {
                        return "--"
                    }
                })();
                _summary.n_lastest_m_return = (function () {
                    let val = _summary.n_lastest_m_return;
                    if (val) {
                        let _val = accLess(val, 0, true);
                        return numberToFixed(_val) + "%"
                    } else {
                        return "--"
                    }
                })();
                this.setData({_summary: _summary})
            }
        }
    },


    /**
     * 组件的方法列表
     */
    methods: {
        bindItem: function (e) {
            let isLink = this.data.isLink;
            let id = e.currentTarget.dataset.id;
            if (!isLink) {return false}
            wx.navigateTo({
                url: "/packageProduct/pages/detail/index?id=" + id
            })
        },
        checkUser: function () {
            let userInfo = getStorageSync("userInfo");
            this.setData({_showPoint: userInfo && userInfo.type})
        }
    }
})
